<template>
	<view class="login">
		<block v-if="styleLoginType === 'one'">
			<view class="container">
				<!--顶部返回按钮-->
				<text class="back-btn iconfont iconzuo" @tap="navBack"></text>
				<!--插画-->
				<view class="right-top-sign"></view>
				<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
				<view class="wrapper">
					<view class="left-top-sign">LOGIN</view>
					<view class="welcome">
						欢迎回来！
					</view>
					<view class="input-content">
						<view class="input-item">
							<text class="tit">用户名称</text>
							<input type="text" name="mobile" v-model="loginParams.beforeUserName" placeholder="用户名称"
								 @blur="blurMobileChange" />
						</view>
						<view class="input-item" v-if="loginByPass">
							<text class="tit">密码</text>
							<input name="password" type="password" v-model="loginParams.beforePassword" placeholder="请输入密码"
								maxlength="20" />
						</view>

						<button class="confirm-btn" :class="'bg-' + themeColor.name" :disabled="btnLoading"
							:loading="btnLoading" @tap="toLogin">
							登录
						</button>
					</view>
					<view @tap="showLoginBySmsCode" class="forget-section">
						{{ loginByPass ? '验证码登录' : '密码登录' }}
					</view>
					<view class="forget-section" @tap="navTo('/pages/public/password')">
						忘记密码?
					</view>
				</view>
				<view class="register-section">
					还没有账号?
					<text @tap="navTo('/pages/public/register')">马上注册</text>
					或者
					<text @tap="toHome">返回主页</text>
				</view>
			</view>
		</block>
		<view class="login-type-2" v-if="styleLoginType === 'two'">
			<!--顶部返回按钮-->
			<text class="back-btn iconfont iconzuo" @tap="navBack"></text>
			<view class="login-top" :class="'bg-' + themeColor.name">
				<view class="desc">
					<view class="title">Hi~</view>
					<text>{{ appName }}欢迎您</text>
				</view>
				<image class="login-pic" :src="loginPic"></image>
			</view>
			<view class="login-type-content">
				<image class="login-bg" :src="loginBg" :style="{height: tabCurrentIndex === 1 ? '150vw' : '94vw'}">
				</image>
				<view class="main">
					<view class="nav-bar">
						<view class="nav-bar-item" v-for="(item, index) in typeList" :key="index"
							:class="tabCurrentIndex === index ? `text-${themeColor.name} nav-bar-item-active` : ''"
							@tap="tabClick(index)">
							{{ item.text }}
						</view>
					</view>
					<block v-if="tabCurrentIndex === 0">
						<view class="login-type-form">
							<view class="input-item">
								<text class="iconfont iconzhanghuffffffpx" :class="'text-' + themeColor.name"></text>
								<input class="login-type-input" type="text" name="mobile" v-model="loginParams.beforeUserName"
									placeholder="请输入用户名称"  @blur="blurMobileChange" />
							</view>
							<view class="input-item" v-if="loginByPass">
								<text class="iconfont iconmimaffffffpx" :class="'text-' + themeColor.name"></text>
								<input class="login-type-input" type="password" v-model="loginParams.beforePassword"
									placeholder="请输入密码" maxlength="20" />
							</view>
							<view class="input-item input-item-sms-code" v-if="!loginByPass">
								<text class="iconfont iconyanzhengma" :class="'text-' + themeColor.name"></text>
							</view>
						</view>
						<view class="login-type-tips">
							<view @tap="showLoginBySmsCode" class="forget-section">
								{{ loginByPass ? '邮箱验证码登录' : '密码登录' }}
							</view>
							<text @tap="navTo('/pages/public/password')">忘记密码？</text>
						</view>
						<button class="confirm-btn" :class="'bg-' + themeColor.name" :disabled="btnLoading"
							:loading="btnLoading" @tap="toLogin">
							登录
						</button>
					</block>
					<block v-if="tabCurrentIndex === 1">
						<view class="login-type-form">
							<view class="input-item">
								<text class="iconfont icondianhua" :class="'text-' + themeColor.name"></text>
								<input class="login-type-input" type="text" name="email" v-model="registerParams.email"
									placeholder="请输入邮箱账号" />

							</view>
							<view class="input-item input-item-sms-code">
								<text class="iconfont iconyanzhengma" :class="'text-' + themeColor.name"></text>
								<view class="input-wrapper">
									<view class="rf-input-wrapper">
										<input type="number" class="login-type-input" v-model="registerParams.code"
											placeholder="请输入验证码" maxlength="6" />
									</view>
									<view class="obtain" @click="countdown">{{time}}</view>
								</view>
							</view>
					
						</view>
						<button class="confirm-btn" :class="'bg-' + themeColor.name" :disabled="btnLoading"
							:loading="btnLoading" @tap="toRegister">
							注册
						</button>
					</block>
				</view>
			</view>
			<view class="login-type-bottom" :class="'text-' + themeColor.name">
				{{ appName }} 版权所有
			</view>
		</view>
	</view>
</template>
<script>
	import {
		loginByPass,
		loginBySmsCode,
		smsCode,
		authLogin,
		registerByPass
	} from '@/api/login';
	import moment from '@/common/moment';
	import request from '../../utils/request.js'
	export default {
		data() {
			return {
				loginParams: {
					beforeUserName:'',
					beforePassword: ''
				},
				registerParams: {
					email:'',
					code:''
				},
				btnLoading: false,
				reqBody: {},
				codeSeconds: 0, // 验证码发送时间间隔
				loginByPass: true,
				smsCodeBtnDisabled: true,
				userInfo: null,
				time: "获取",
				loginBg: this.$mAssetsPath.loginBg,
				loginPic: this.$mAssetsPath.loginPic,
				appName: '芸上微庄园',
				styleLoginType: this.$mSettingConfig.styleLoginType,
				closeRegisterPromoCode: this.$mSettingConfig.closeRegisterPromoCode,
				tabCurrentIndex: 0,
				typeList: [{
						text: '登录'
					},
					{
						text: '注册'
					}
				]
			};
		},
		onShow() {
			if (this.$mStore.getters.hasLogin) {
				this.$mRouter.reLaunch({
					route: '/pages/index/index'
				});
			}
		},
		onLoad(options) {
			this.tabCurrentIndex = parseInt(options.type || 0, 10);
			const time =
				moment().valueOf() / 1000 - uni.getStorageSync('loginSmsCodeTime');
			if (time < 60) {
				this.codeSeconds =
					this.$mConstDataConfig.sendCodeTime - parseInt(time, 10);
				this.handleSmsCodeTime(this.codeSeconds);
			} else {
				this.codeSeconds = this.$mConstDataConfig.sendCodeTime;
				this.smsCodeBtnDisabled = false;
				uni.removeStorageSync('loginSmsCodeTime');
			}
			this.registerParams.promoCode = options.promo_code;
			this.userInfo = uni.getStorageSync('wechatUserInfo');
		},
		methods: {
			// 发送验证码并进入倒计时
			countdown() {
				var that = this
				that.time = 60
				if (that.time > 0) {
					var interval = setInterval(function() {
						that.time--
						if (that.time == 0) {
							clearInterval(interval)
							that.time = "重新获取"
						}
					}, 1000)
				}
				request({
					url: '/before/bgs/before-user/ThreeSend',
					data: {
						email: this.registerParams.email
					},
					methods: 'Get',
					success: (res) => {
						console.log(res.data.message)
						if (res.data.message == "邮箱已存在") {
							uni.showToast({
								title: '邮箱已存在',
								icon: 'success'
							})
						}
						if (res.data.message == "成功") {
							uni.showToast({
								title: '发送成功',
								icon: 'success'
							})
						}
					}
				})
			},
			// 切换登录/注册
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			toRegister() {
				request({
					url: '/before/bgs/before-user/ThreeZhuCe',
					data: {
						email: this.registerParams.email,
						code: this.registerParams.code
					},
					methods: 'Get',
					success: (res) => {
						uni.showToast({
							title: '注册成功',
							icon: 'success'
						})
						uni.setStorage({
							key: 'beforeUser',
							data: res.data,
							success() {
								console.log("存储成功");
								console.log(res.data)
							}
						})
						//页面进行跳转
						setTimeout(() => {
							uni.reLaunch({
								url: "../profile/profile"
							})
						}, 1000)
					}
				})
			},
			//登录
			toLogin(){
				if (this.loginParams.beforeUserName.length == 0) {
					//ti shi xin xi
					uni.showToast({
						icon: 'none',
						title: '用户名不能为空',
					});
					return;
				}
				//验证密码不能为空
				if (this.loginParams.beforePassword.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '密码不能为空',
					});
					return;
				}
				//验证密码长度
				if (this.loginParams.beforePassword.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码长度不能小于6',
					});
					return;
				}
				//调用后台登录方法
				request({
					url: '/before/bgs/before-user/ThreeBeforeLogin',
					data: this.loginParams,
					method: 'Get',
					success: (res) => {
						console.log(res.data);
						//判断返回值
						if (res.data.success == false) {
							uni.showToast({
								title: res.data.message,
								icon: 'error'
							})
						}
						if (res.data.message == '成功') {
							uni.showToast({
								title: '登录成功',
								icon: 'success'
							})
							//将登陆信息进行存值
							//getApp().globalData.userInfo = res.data.data.map.user
				
							uni.setStorage({
								key: 'beforeUser',
								data: res.data.data.map.user,
								success() {
									console.log("存储成功");
								}
							})
							//页面进行跳转
							setTimeout(() => {
								uni.reLaunch({
									url:"../index/index"
								})
							}, 1000)
						}
					}
				})
			}
			// 注册账号
			// async toRegister(registerParams,Code) {
			// 	if (this.$mSettingConfig.closeRegister) {
			// 		this.$mHelper.toast('暂未开放注册，敬请期待～');
			// 		return;
			// 	}
			// 	this.reqBody['email'] = this.registerParams['beforeUserEmail'];
			// 	this.reqBody['password'] = this.registerParams['beforePassword'];
			// 	this.reqBody['code'] = this.Code;
			// 	this.reqBody['nickname'] = this.registerParams['beforeUserNickName'];
			// 	// const cheRes = this.$mGraceChecker.check(
			// 	// 	this.reqBody,
			// 	// 	this.$mFormRule.registerRule
			// 	// );
			// 	// if (!cheRes) {
			// 	// 	this.$mHelper.toast(this.$mGraceChecker.error);
			// 	// 	return;
			// 	// }
			// 	// if (
			// 	// 	this.registerParams['password'] !==
			// 	// 	this.password_repetition
			// 	// ) {
			// 	// 	this.$mHelper.toast('两次输入的密码不一致');
			// 	// 	return;
			// 	// }
				
			// 	this.btnLoading = true;
			// 	this.reqBody.group = this.$mHelper.platformGroupFilter();
			// 	 request({
			// 		url: '/before/bgs/before-user/ThreeZhuCe',
			// 		data: {
			// 			beforeUser: this.registerParams,
			// 			code: this.Code
			// 		},
			// 		methods: 'get',
			// 		success: (res) => {
			// 			this.btnLoading = false;
			// 			this.$mHelper.toast('恭喜您注册成功');
			// 			uni.setStorageSync('loginMobile', this.reqBody['email']);
			// 			uni.setStorageSync('loginPassword', this.reqBody['password']);
			// 			this.$mRouter.push({ route: '/pages/public/login' });
			// 		}
			// 	}).catch(() => {
			// 			this.btnLoading = false;
			// 		});
			// }
		}
	};
</script>
<style lang="scss">
	page {
		background: $color-white;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		overflow: hidden;
		background: #fff;

		.wrapper {
			position: relative;
			z-index: 90;
			background: #fff;
			padding-bottom: 40upx;
		}

		.back-btn {
			position: absolute;
			left: 40upx;
			z-index: 9999;
			padding-top: var(--status-bar-height);
			top: 40upx;
			font-size: 40upx;
			color: $font-color-dark;
		}

		.left-top-sign {
			font-size: 120upx;
			color: $page-color-base;
			position: relative;
			left: -16upx;
		}

		.right-top-sign {
			position: absolute;
			top: 80upx;
			right: -30upx;
			z-index: 95;

			&:before,
			&:after {
				display: block;
				content: '';
				width: 400upx;
				height: 80upx;
				background: #b4f3e2;
			}

			&:before {
				transform: rotate(50deg);
				border-radius: 0 50px 0 0;
			}

			&:after {
				position: absolute;
				right: -198upx;
				top: 0;
				transform: rotate(-50deg);
				border-radius: 50px 0 0 0;
				/* background: pink; */
			}
		}

		.left-bottom-sign {
			position: absolute;
			left: -270upx;
			bottom: -320upx;
			border: 100upx solid #d0d1fd;
			border-radius: 50%;
			padding: 180upx;
		}

		.welcome {
			position: relative;
			left: 50upx;
			top: -90upx;
			font-size: 46upx;
			color: #555;
			text-shadow: 1px 0px 1px rgba(0, 0, 0, 0.3);
		}

		.input-content {
			padding: 0 60upx;
		}

		.input-item {
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			justify-content: center;
			padding: 0 30upx;
			background: $page-color-light;
			height: 120upx;
			border-radius: 4px;
			margin-bottom: 50upx;

			&:last-child {
				margin-bottom: 0;
			}

			.tit {
				height: 50upx;
				line-height: 56upx;
				font-size: $font-sm + 2upx;
				color: $font-color-base;
			}

			input {
				height: 60upx;
				font-size: $font-base + 2upx;
				color: $font-color-dark;
				width: 100%;
			}
		}

		.input-item-sms-code {
			position: relative;
			width: 100%;

			.sms-code-btn {
				position: absolute;
				color: #111;
				right: 20upx;
				bottom: 20upx;
				font-size: 28upx;
			}

			.sms-code-resend {
				color: #999;
			}

			.sms-code-btn:after {
				border: none;
				background-color: transparent;
			}
		}

		.forget-section {
			font-size: $font-sm + 2upx;
			color: $font-color-spec;
			text-align: center;
			margin-top: 40upx;
		}

		.register-section {
			margin: 30upx 0 50upx 0;
			width: 100%;
			font-size: $font-sm + 2upx;
			color: $font-color-base;
			text-align: center;

			text {
				color: $font-color-spec;
				margin-left: 10upx;
			}

			text:first-child {
				margin-right: 10upx;
			}
		}

		.btn-group {
			display: flex;
			margin-bottom: 20upx;
		}
	}

	.login-type-2 {
		width: 100%;
		position: relative;

		.back-btn {
			position: absolute;
			left: 40upx;
			z-index: 9999;
			padding-top: var(--status-bar-height);
			top: 40upx;
			font-size: 48upx;
			color: $color-white;
		}

		.login-top {
			height: 460upx;
			position: relative;

			.desc {
				position: absolute;
				top: 200upx;
				left: 40upx;
				font-size: 48upx;

				.title {
					font-size: 48upx;
				}
			}

			.login-pic {
				position: absolute;
				width: 220upx;
				height: 270upx;
				right: 30upx;
				top: 100upx;
			}
		}

		.login-type-content {
			position: relative;
			top: -72upx;

			.login-bg {
				width: 94vw;
				height: 94vw;
				margin: 0 3vw;
			}

			.main {
				width: 94vw;
				position: absolute;
				top: 0;
				left: 3vw;

				.nav-bar {
					display: flex;
					height: 100upx;
					justify-content: center;
					align-items: center;
					position: relative;
					z-index: 10;

					.nav-bar-item {
						flex: 1;
						display: flex;
						height: 100%;
						line-height: 96upx;
						font-size: $font-lg;
						display: flex;
						margin: 0 120upx;
						justify-content: center;
					}

					.nav-bar-item-active {
						border-bottom: 5upx solid;
					}
				}

				.login-type-form {
					width: 80%;
					margin: 50upx auto;

					.input-item {
						position: relative;
						height: 90upx;
						line-height: 90upx;
						margin-bottom: $spacing-lg;

						.iconfont {
							font-size: 50upx;
							position: absolute;
							left: 0;
						}

						.login-type-input {
							height: 90upx;
							padding-left: 80upx;
							border-bottom: 1upx solid rgba(0, 0, 0, .1);
						}

						.sms-code-btn,
						sms-code-resend {
							width: 240upx;
							font-size: $font-base - 2upx;
						}
					}
				}

				.login-type-tips {
					padding: 0 50upx;
					display: flex;
					justify-content: space-between;
				}

				.confirm-btn {
					height: 80upx;
					line-height: 80upx;
				}
			}
		}

		.login-type-bottom {
			width: 100%;
			padding-bottom: 30upx;
			text-align: center;
			font-size: $font-lg;
		}

	}
</style>
